const path = require("path");
// 直接使用webpack插件系统
const package = require("./package.json");

module.exports = {
  mode: "production",
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "arenanext.user.js",
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
  plugins: [
    // 使用自定义插件函数来生成userscript头部
    {
      apply: (compiler) => {
        compiler.hooks.emit.tap("UserscriptHeaderPlugin", (compilation) => {
          const headers = `// ==UserScript==
// @name         ArenaNext
// @namespace    https://box3lab.com
// @version      ${package.version}
// @description  下一代的神岛AI编辑器
// @author       神岛实验室
// @match        https://dao3.fun/edit/*
// @match        https://view.dao3.fun/e/*
// @match        https://dao3.fun/edu/edit/*
// @match        https://view.edu.dao3.fun/e/*
// @icon         https://static.codemao.cn/pickduck/H1IEFgiggl.png?hash=Fq_F3T0XBmWVLXGsz7D2Npss5nNh
// @grant        unsafeWindow
// @license      Apache2.0
// @require      https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js
// @require      https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/marked/4.0.2/marked.min.js
// @require      https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/11.4.0/highlight.min.js
// @downloadURL  https://static.pgaot.com/Assets/js/arenanext.user.js
// @updateURL    https://static.pgaot.com/Assets/js/arenanext.user.js
// ==/UserScript==

`;

          // 获取主JS文件资源
          const mainAsset = compilation.assets["arenanext.user.js"];
          // 将头部添加到文件内容前面
          compilation.assets["arenanext.user.js"] = {
            source: () => headers + mainAsset.source(),
            size: () => headers.length + mainAsset.size(),
          };
        });
      },
    },
  ],
  externals: {
    jquery: "$",
    marked: "marked",
    "highlight.js": "hljs",
  },
};
